using Cinemachine;
using UnityEngine;

public class PlayerFx : EntityFX
{
    private Player player;
    [Header("After image fx")]
    [SerializeField] private GameObject afterImagePrefab;
    [SerializeField] private float colorLooseRate;
    [SerializeField] private float afterImageCooldown;
    private float afterImageCooldownTimer;

    [Header("Screen shake fx")]
    private CinemachineImpulseSource screenShake;
    [SerializeField] private float shakeMultiplier;
    public Vector3 shakeSwordImpact;
    public Vector3 shakeHighDamage;
    [Space]
    private ParticleSystem dustFx;

    protected override void Awake()
    {
        base.Awake();
        player = GetComponent<Player>();
        screenShake = GetComponent<CinemachineImpulseSource>();
        dustFx = transform.Find("DustFx")?.GetComponent<ParticleSystem>();
    }

    private void Update()
    {
        afterImageCooldownTimer -= Time.deltaTime;
    }

    public void CreateAfterImage()
    {
        if (!(afterImageCooldownTimer < 0))
            return;
        afterImageCooldownTimer = afterImageCooldown;
        GameObject obj = Instantiate(afterImagePrefab, transform.position + Vector3.up * 0.336f, transform.rotation);
        obj.GetComponent<AfterImageFx>().Setup(colorLooseRate, Sr.sprite);
    }

    public void ScreenShake(Vector3 shakePower)
    {
        screenShake.m_DefaultVelocity = new Vector3(shakePower.x * player.FacingDirection, shakePower.y) * shakeMultiplier;
        screenShake.GenerateImpulse();
    }

    public void PlayDustFx()
    {
        dustFx.Play();
    }
}
